package com.zx.dsptuneup_cloud.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import com.gyf.barlibrary.ImmersionBar;
import com.jyn.vcview.VerificationCodeView;
import com.orhanobut.hawk.Hawk;
import com.zx.dsptuneup_cloud.App;
import com.zx.dsptuneup_cloud.Base.BaseActivity;
import com.zx.dsptuneup_cloud.R;
import com.zx.dsptuneup_cloud.Tools.CVIPaperDialogUtils;
import com.zx.dsptuneup_cloud.Util.ByteToString;
import com.zx.dsptuneup_cloud.Util.CommonAction;
import com.zx.dsptuneup_cloud.Util.Constants;
import com.zx.dsptuneup_cloud.Util.Netty.NettyListener;
import com.zx.dsptuneup_cloud.Util.UserUtil;
import butterknife.BindView;

public class SettingActivity extends BaseActivity implements NettyListener, VerificationCodeView.OnCodeFinishListener {
    @BindView(R.id.ic_nav)
    ImageView Nav_left;
    @BindView(R.id.title)
    RelativeLayout navBar;
    @BindView(R.id.setting_rl_ll1)
    RelativeLayout rl11;

    @BindView(R.id.setting_rl_ll4)
    RelativeLayout rl44;
    @BindView(R.id.setting_rl_ll6)
    RelativeLayout rl6;
    @BindView(R.id.setting_rl_ll8)
    RelativeLayout rl8;
    @BindView(R.id.device_number)
    TextView device_number;
    @BindView(R.id.version)
    TextView version;
    @BindView(R.id.Switch_hotpot)
    RelativeLayout switch_hotpot;
    @BindView(R.id.setting_text_1)
    TextView setting_text_1;
    private Dialog joinMeetingDialog;
    private VerificationCodeView join_invite_codeview;
    private RelativeLayout tips;
    private String device_num;
    private String version_num;
    private final String TAG = "SettingActivity.class";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initUI();
    }

    @Override
    public void onTextChange(View view, String content) {
        if (view == join_invite_codeview && content.length()<4) {
            tips.setVisibility(View.INVISIBLE);
        }

    }

    @Override
    public void onComplete(View view, String content) {
        Log.d("codeEditView","//输入内容改变后的回调===="+content);
        if (content.length() <= 0) {
            Toast.makeText(SettingActivity.this,"输入不能为空",Toast.LENGTH_LONG);
            return;
        }else if(content.length() == 4){
            if(content.equals(UserUtil.DevelopCode)){
                Hawk.put(UserUtil.DevelopMode, true);
                UserUtil.isDevelopMode = true;

                InputMethodManager imm = (InputMethodManager) joinMeetingDialog.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(joinMeetingDialog.getWindow().getDecorView().getWindowToken(), 0); // 解决键盘无法关闭问题

                joinMeetingDialog.dismiss();
                Toast.makeText(SettingActivity.this,"开发者模式切换成功!",Toast.LENGTH_SHORT).show();
                refreshUI();
            }else {
                tips.setVisibility(View.VISIBLE);
                return;
            }
        }else{
            tips.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 开发者模式操作权限码
     */
    private void joinMeetingDialog() {
        joinMeetingDialog = new Dialog(SettingActivity.this, R.style.update_dialog);
        View view = LayoutInflater.from(SettingActivity.this).inflate(R.layout.diaog_verify_invite_code, null);//加载自己的布局
        join_invite_codeview = view.findViewById(R.id.verificationcodeview);
        join_invite_codeview.setOnCodeFinishListener(this);
        ImageView close_img = view.findViewById(R.id.ima_close);
        ImageView imageView = view.findViewById(R.id.clear_ima);
        tips = (RelativeLayout) view.findViewById(R.id.tipInfo);
        tips.setVisibility(View.INVISIBLE);
        joinMeetingDialog.setContentView(view);//这里还可以指定布局参数
        joinMeetingDialog.setCancelable(false);// 不可以用“返回键”取消
        joinMeetingDialog.show();
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                join_invite_codeview.setEmpty();
            }
        });
        close_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                joinMeetingDialog.dismiss();
            }
        });
    }

    private void refreshUI() {
        if(UserUtil.isDevelopMode){
            setting_text_1.setText("开发者模式");
        }else {
            setting_text_1.setText("普通模式");
        }
    }

    protected void initUI() {
        ImmersionBar.with(this)
                .transparentBar()
                .statusBarColor(R.color.home_bg)
                .statusBarDarkFont(false)
                .init();

        refreshUI();
        if (Hawk.contains("device_version")){
            String device_version=Hawk.get("device_version");
         String cc=ByteToString.strTo10(device_version.substring(device_version.length()-2,device_version.length()))+"";
          String bb= ByteToString.strTo10(device_version.substring(device_version.length()-4,device_version.length()-2))+"";
            //版本
           version.setText(bb+"."+cc);
            Log.d("TAGevent回复=heartBeatData2222", "  编号和版本="+device_version+
                    "  编号"+device_version.substring(0,device_version.length()-4)+"   版本"+device_version.substring(device_version.length()-4,device_version.length()));
           //设备编号
            if ("0001".equals(device_version.substring(0,device_version.length()-4))){
                device_number.setText("C30");
                rl6.setVisibility(View.VISIBLE);
                //device_number.setText(ByteToString.strTo10(device_version.substring(0,device_version.length()-4)));
            }else {
                rl6.setVisibility(View.GONE);

            }
        }

        rl11.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(UserUtil.isDevelopMode){
                    CVIPaperDialogUtils.showCustomDialog(SettingActivity.this, "切回普通模式?", null, "确定", true, new CVIPaperDialogUtils.ConfirmDialogListener() {
                        @Override
                        public void onClickButton(boolean clickConfirm, boolean clickCancel) {
                            if (clickConfirm){
                                Hawk.put(UserUtil.DevelopMode, false);
                                UserUtil.isDevelopMode = false;
                                Toast.makeText(SettingActivity.this,"普通模式切换成功!",Toast.LENGTH_SHORT).show();
                                refreshUI();
                            }
                        }
                    });


                }else {
                    joinMeetingDialog();
                }
            }
        });

        rl44.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CVIPaperDialogUtils.showCustomDialog(SettingActivity.this, "是否恢复出厂设置?", null, "确定", true, new CVIPaperDialogUtils.ConfirmDialogListener() {
                    @Override
                    public void onClickButton(boolean clickConfirm, boolean clickCancel) {
                        if (clickConfirm){
                            App.getContext().getProtocolServiceUtil().factoryReset();
                           //App.getContext().getProtocolConversion().setReboot();
                            Toast.makeText(SettingActivity.this,"恢复出厂设置成功!",Toast.LENGTH_SHORT).show();
                        }
                    }
                });

            }
        });


        // 切换热点
        switch_hotpot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
                builder.setMessage("确定要切换热点吗");
                builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        App.getContext().getNettyClient().disconnect();
                        CommonAction.getInstance().OutSign();
                        startActivity(new Intent(SettingActivity.this, LoginActivity.class));
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.create().show();
            }
        });
        // 返回
        Nav_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    @Override
    public void onMessageReaderTimeout() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
            }
        });
    }

    /*
     * 回调客户端接收的信息
     */
    @Override
    public void onMessageResponse(final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(msg.equals(Constants.SetOkEnum.RESTORE)){
                    Toast.makeText(SettingActivity.this, "恢复成功!", Toast.LENGTH_SHORT).show();
                }
//                Toast.makeText(SettingActivity.this, "接收成功" + msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    //连接状态变化的时候 会走这
    @Override
    public void onServiceStatusConnectChanged(final int statusCode) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (statusCode == NettyListener.STATUS_CONNECT_SUCCESS) {
                    Log.e(TAG, "STATUS_CONNECT_SUCCESS:");
                    if (App.getContext().getNettyClient().getConnectStatus()) {
                    }
                } else {
                    Log.e(TAG, "onServiceStatusConnectChanged:" + statusCode);
                    // 连接断开
                    Toast.makeText(SettingActivity.this, "连接断开", Toast.LENGTH_LONG).show();
                    App.getContext().getNettyClient().disconnect();
                    CommonAction.getInstance().OutSign();
                    startActivity(new Intent(SettingActivity.this, LoginActivity.class));
                }
            }
        });
    }
}
